{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# requests(让HTTPS服务人类)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* r是指response响应"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "r=requests.get(\"https://cn.bing.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* r.content\n",
    "\n",
    "> 1.可以获取请求数据内容（观察页面数据形态 HTML,json）\n",
    "\n",
    "> 2.b是指bytes:二进制文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "r=requests.get(\"https://httpbin.org/get\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\\n  \"args\": {}, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.24.0\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-6242d85c-24acaf394d99e4a81bf5b319\"\\n  }, \\n  \"origin\": \"120.85.40.178\", \\n  \"url\": \"https://httpbin.org/get\"\\n}\\n'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.24.0',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-6242d85c-24acaf394d99e4a81bf5b319'},\n",
       " 'origin': '120.85.40.178',\n",
       " 'url': 'https://httpbin.org/get'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json() #将json数据转换为字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\\n  \"args\": {}, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.24.0\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-6242d85c-24acaf394d99e4a81bf5b319\"\\n  }, \\n  \"origin\": \"120.85.40.178\", \\n  \"url\": \"https://httpbin.org/get\"\\n}\\n'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# payload\n",
    "使用payload来实现请求数据传递"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "payload={\n",
    "    'name':'zhichao',\n",
    "    'JOB':'IT'\n",
    "}\n",
    "r=requests.get(\"https://httpbin.org/get\",params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {'JOB': 'IT', 'name': 'zhichao'},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.24.0',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-6242d870-706c774f5e81f11318b9edee'},\n",
       " 'origin': '120.85.40.178',\n",
       " 'url': 'https://httpbin.org/get?name=zhichao&JOB=IT'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# face++ detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.准备base url (对应该功能的url链接)\n",
    "face_detect_url=\"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 2.准备参数数据\n",
    "payload={\n",
    "    'api_key':'U-d6Okv9dUcg-ZQVJhxz7Ztgxvzn9YJJ',\n",
    "    'api_secret':'KUd9SBw4KX7AzVaFCZ4ngqB4r4-AJND8',\n",
    "    'image_url':'https://img.alicdn.com/tfs/TB1A7Kwzq61gK0jSZFlXXXDKFXa-764-1146.png',\n",
    "    'return_landmark':1,\n",
    "    'return_attributes':'gender,age,smiling,emotion'\n",
    "}\n",
    "\n",
    "# 3.检查调用的请求方法：POST\n",
    "# 4.请求人脸数据\n",
    "r=requests.post(url=face_detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648547959,de1eb132-0a50-49ae-a06b-5cb72e5d529b',\n",
       " 'time_used': 466,\n",
       " 'faces': [{'face_token': 'b7bfc3290078c0ff2386d12d7be4e8f2',\n",
       "   'face_rectangle': {'top': 338, 'left': 142, 'width': 496, 'height': 496},\n",
       "   'landmark': {'contour_chin': {'x': 394, 'y': 828},\n",
       "    'contour_left1': {'x': 141, 'y': 444},\n",
       "    'contour_left2': {'x': 147, 'y': 498},\n",
       "    'contour_left3': {'x': 159, 'y': 550},\n",
       "    'contour_left4': {'x': 176, 'y': 600},\n",
       "    'contour_left5': {'x': 194, 'y': 649},\n",
       "    'contour_left6': {'x': 220, 'y': 696},\n",
       "    'contour_left7': {'x': 251, 'y': 739},\n",
       "    'contour_left8': {'x': 288, 'y': 779},\n",
       "    'contour_left9': {'x': 333, 'y': 813},\n",
       "    'contour_right1': {'x': 637, 'y': 435},\n",
       "    'contour_right2': {'x': 634, 'y': 491},\n",
       "    'contour_right3': {'x': 626, 'y': 544},\n",
       "    'contour_right4': {'x': 614, 'y': 597},\n",
       "    'contour_right5': {'x': 598, 'y': 650},\n",
       "    'contour_right6': {'x': 576, 'y': 699},\n",
       "    'contour_right7': {'x': 545, 'y': 744},\n",
       "    'contour_right8': {'x': 506, 'y': 783},\n",
       "    'contour_right9': {'x': 457, 'y': 815},\n",
       "    'left_eye_bottom': {'x': 262, 'y': 462},\n",
       "    'left_eye_center': {'x': 267, 'y': 446},\n",
       "    'left_eye_left_corner': {'x': 223, 'y': 441},\n",
       "    'left_eye_lower_left_quarter': {'x': 238, 'y': 455},\n",
       "    'left_eye_lower_right_quarter': {'x': 291, 'y': 460},\n",
       "    'left_eye_pupil': {'x': 273, 'y': 440},\n",
       "    'left_eye_right_corner': {'x': 317, 'y': 457},\n",
       "    'left_eye_top': {'x': 266, 'y': 424},\n",
       "    'left_eye_upper_left_quarter': {'x': 240, 'y': 428},\n",
       "    'left_eye_upper_right_quarter': {'x': 295, 'y': 434},\n",
       "    'left_eyebrow_left_corner': {'x': 176, 'y': 372},\n",
       "    'left_eyebrow_lower_left_quarter': {'x': 211, 'y': 374},\n",
       "    'left_eyebrow_lower_middle': {'x': 248, 'y': 380},\n",
       "    'left_eyebrow_lower_right_quarter': {'x': 285, 'y': 390},\n",
       "    'left_eyebrow_right_corner': {'x': 324, 'y': 394},\n",
       "    'left_eyebrow_upper_left_quarter': {'x': 211, 'y': 348},\n",
       "    'left_eyebrow_upper_middle': {'x': 254, 'y': 350},\n",
       "    'left_eyebrow_upper_right_quarter': {'x': 293, 'y': 365},\n",
       "    'mouth_left_corner': {'x': 316, 'y': 697},\n",
       "    'mouth_lower_lip_bottom': {'x': 392, 'y': 735},\n",
       "    'mouth_lower_lip_left_contour1': {'x': 354, 'y': 698},\n",
       "    'mouth_lower_lip_left_contour2': {'x': 335, 'y': 716},\n",
       "    'mouth_lower_lip_left_contour3': {'x': 359, 'y': 730},\n",
       "    'mouth_lower_lip_right_contour1': {'x': 429, 'y': 694},\n",
       "    'mouth_lower_lip_right_contour2': {'x': 450, 'y': 710},\n",
       "    'mouth_lower_lip_right_contour3': {'x': 425, 'y': 727},\n",
       "    'mouth_lower_lip_top': {'x': 390, 'y': 699},\n",
       "    'mouth_right_corner': {'x': 470, 'y': 689},\n",
       "    'mouth_upper_lip_bottom': {'x': 390, 'y': 699},\n",
       "    'mouth_upper_lip_left_contour1': {'x': 369, 'y': 673},\n",
       "    'mouth_upper_lip_left_contour2': {'x': 341, 'y': 684},\n",
       "    'mouth_upper_lip_left_contour3': {'x': 354, 'y': 698},\n",
       "    'mouth_upper_lip_right_contour1': {'x': 408, 'y': 671},\n",
       "    'mouth_upper_lip_right_contour2': {'x': 440, 'y': 679},\n",
       "    'mouth_upper_lip_right_contour3': {'x': 429, 'y': 694},\n",
       "    'mouth_upper_lip_top': {'x': 389, 'y': 677},\n",
       "    'nose_contour_left1': {'x': 347, 'y': 462},\n",
       "    'nose_contour_left2': {'x': 333, 'y': 567},\n",
       "    'nose_contour_left3': {'x': 349, 'y': 625},\n",
       "    'nose_contour_lower_middle': {'x': 383, 'y': 635},\n",
       "    'nose_contour_right1': {'x': 416, 'y': 461},\n",
       "    'nose_contour_right2': {'x': 433, 'y': 566},\n",
       "    'nose_contour_right3': {'x': 417, 'y': 625},\n",
       "    'nose_left': {'x': 320, 'y': 610},\n",
       "    'nose_right': {'x': 447, 'y': 609},\n",
       "    'nose_tip': {'x': 382, 'y': 600},\n",
       "    'right_eye_bottom': {'x': 503, 'y': 462},\n",
       "    'right_eye_center': {'x': 499, 'y': 447},\n",
       "    'right_eye_left_corner': {'x': 447, 'y': 460},\n",
       "    'right_eye_lower_left_quarter': {'x': 474, 'y': 462},\n",
       "    'right_eye_lower_right_quarter': {'x': 528, 'y': 455},\n",
       "    'right_eye_pupil': {'x': 497, 'y': 438},\n",
       "    'right_eye_right_corner': {'x': 546, 'y': 442},\n",
       "    'right_eye_top': {'x': 498, 'y': 423},\n",
       "    'right_eye_upper_left_quarter': {'x': 468, 'y': 434},\n",
       "    'right_eye_upper_right_quarter': {'x': 526, 'y': 427},\n",
       "    'right_eyebrow_left_corner': {'x': 437, 'y': 389},\n",
       "    'right_eyebrow_lower_left_quarter': {'x': 478, 'y': 385},\n",
       "    'right_eyebrow_lower_middle': {'x': 517, 'y': 375},\n",
       "    'right_eyebrow_lower_right_quarter': {'x': 555, 'y': 367},\n",
       "    'right_eyebrow_right_corner': {'x': 591, 'y': 362},\n",
       "    'right_eyebrow_upper_left_quarter': {'x': 471, 'y': 360},\n",
       "    'right_eyebrow_upper_middle': {'x': 511, 'y': 347},\n",
       "    'right_eyebrow_upper_right_quarter': {'x': 554, 'y': 342}},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 20},\n",
       "    'smile': {'value': 0.143, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.002,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 0.005,\n",
       "     'neutral': 99.957,\n",
       "     'sadness': 0.006,\n",
       "     'surprise': 0.029}}}],\n",
       " 'image_id': 'PqqlcFK4HC1ED1mjK+11yg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
